<?php

namespace app\backend\controller;

use app\common\model\Goods as GoodsModel;
use app\common\model\GoodsCategory as GoodsCategoryModel;
use app\common\model\Business as businessModel;
use think\Controller;
use think\Validate;
use think\Request;
use think\facade\Env;

class Goods extends Controller
{
    public function index()
    {
        return $this->fetch();
    }

    public function shopment()
    {
        return $this->fetch();
    }

    public function goodsList()
    {
        $count = GoodsModel::count();
        $goods_name=request()->param('goods_name');

        $limit = request()->param('limit');
        if($goods_name){

            $goods_list = GoodsModel::where('name','like','%'.$goods_name.'%')->order('id desc')->paginate($limit);
            foreach($goods_list as $key=>$row){
                $cat_name = GoodsCategoryModel::where('cat_id',$row['cat_id'])->value('cat_name');
                $goods_list[$key]['cat_name'] = $cat_name;
            }

        }else{
            $goods_list = GoodsModel::order('id desc')->paginate($limit);
            foreach($goods_list as $key=>$row){
                $cat_name = GoodsCategoryModel::where('cat_id',$row['cat_id'])->value('cat_name');
                $goods_list[$key]['cat_name'] = $cat_name;
            }


        }
        if ($goods_list) {
            $data = ['code' => 0, 'msg' => '用户列表', 'count' => $count, 'data' => $goods_list->items()];
            return json($data);
        } else {
            $data = ['code' => 0, 'msg' => '用户列表', 'count' => $count, 'data' => array()];
            return json($data);
        }

    }
    /**
     *下架和上架商品
     **/
    public function caozuo_goods(Request $request){
        if($request->isPost()) {
            $id = $request->param('id');
            $res = goodsModel::where(['id'=>$id])->find()->toArray();
            $info = goodsModel::get($res['id']);
            //echo $info->getData('is_on');exit;
            $info['is_on'] = $info->getData('is_on');
            //print_r($res);exit;
            if(empty($res)){
                $data['status'] = 2;
                $data['msg'] = lang("出现异常");
            }else{
                if($info['is_on'] == '是'){
                    $data['is_on'] = 0;
                }else{
                    $data['is_on'] = 1;
                }
                $update = goodsModel::where(['id'=>$id])->update($data);
                //echo goodsModel::getLastSql();exit;
                if($update){
                    $data['status'] = 1;
                    $data['msg'] = lang("操作成功");
                }else{
                    $data['status'] = 2;
                    $data['msg'] = lang("操作失败");
                }
            }
            return $data;
        }
    }
/*    public function detail(Request $request){
        $id =  $request->param('id');
        $info=goodsModel::where('id',$id)->find();
        //echo goodsModel::getLastSql();exit;
        //print_r($info);exit;
        $info['img1'] = explode(',',$info['img1']);
        $name= GoodsCategoryModel::where('cat_id',$info['cat_id'])->field('cat_name')->find();
        $info['cat_name']  = $name['cat_name'];
        $this->assign('info',$info);
        return $this->fetch();
    }*/
     /**
     * 编辑器内容图片
     */
    public function uploadImg()
    {
        $files = request()->file();
        foreach ($files as $file) {
            // 移动到框架应用根目录/uploads/ 目录下
            $info = $file->validate(['size' => 2048000])->move('uploads');
            if ($info) {
                // 成功上传后 获取上传信息
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                $img_name = $info->getSaveName();
            } else {
                // 上传失败获取错误信息
                $img_name = $file->getError();
            }
        }

        $data = ['code' => 0, 'msg' => $img_name, 'data' => ['src' => "/uploads/$img_name"]];
        return json($data);

    }
    /**
    *  上传商品图片
    */
    public function upload_img()
    {
        if(request()->isPost()){
             $res['code']=1;
             $res['msg'] = lang('提交成功');
             $file = request()->file('file');
             $info = $file->move(Env::get('root_path') . 'public/uploads');
             //halt( $info);
             if($info){
                 $res['name'] = $info->getFilename();
                 //$res['filepath'] = 'upload/admin/'.$info->getSaveName();
                 $res['filepath']='/uploads/' . $info->getSaveName();
             }else{
                 $res['code'] = 0;
                 $res['msg'] = lang('提交失败');
             }
             return $res;
        }
    }
    /**
     * 删除商品图片
     **/
    public function del_img(){
        $uid = session('userid');
        $bis_id = businessModel::where('userid',$uid)->find();
        if (request()->isPost()) {
            $id = $_POST['id'];
            $path = $_POST['path'];
            //echo Env::get('root_path').$path;exit;
            $info = goodsModel::where(['id'=> $id,'bis_id'=>$bis_id['id']])->find();
            if($info){
                unlink(Env::get('root_path').'public/'.$path);
                $data['status'] = 1;
                $data['msg'] = lang("删除成功");
            }else{
                $data['status'] = 2;
                $data['msg'] = lang("删除失败");
            }
            return $data;
        }
    }
    /*
    * 添加商品
    */
    public function addGoods(Request $request)
    {
        //分类
        $selectOption = GoodsCategoryModel::where('cat_id','<>',0)->order('cat_id asc')->select()->toArray();
        $this->assign('selectOption',$selectOption);
        return $this->fetch();
    }
    //操作
    public function doadd(Request $request)
    {
        $info = $_POST;
        if($info['cat_id'] == 11){
            //如果商品类型为拼团秒杀
            if(!$info['fighting_price'] || $info['fighting_price'] <=0){
                $data['code'] = 0;
                $data['msg'] = '请填写拼团价';
                return $data;
            }
            if(!$info['people_count'] || $info['people_count'] <=0){
                $data['code'] = 0;
                $data['msg'] = '请填写拼团人数';
                return $data;
            }
            if(!$info['fight_time']){
                $data['code'] = 0;
                $data['msg'] = '请填写拼团到期时间';
                return $data;
            }
        }
        if($info['cat_id'] == 12){
            //如果商品类型为限时秒杀
            if(!$info['price_spike'] || $info['price_spike'] <=0){
                $data['code'] = 0;
                $data['msg'] = '请填写拼团价';
                return $data;
            }
            if(!$info['start_time'] || $info['end_time'] <=0){
                $data['code'] = 0;
                $data['msg'] = '请填写拼团时间';
                return $data;
            }
        }
       if($info['start_time']){
           $info['start_time'] = strtotime($info['start_time']);
       }
        if($info['end_time']){
            $info['end_time'] = strtotime($info['end_time']);
        }
         if(isset($info['pc_src'])){
             $info['img1'] = implode(",",$info['pc_src']);
             unset($info['pc_src']);
         }
        $info['play_start_time'] = strtotime($info['play_start_time']);
        $info['play_end_time'] = strtotime($info['play_end_time']);
        $validate = new \app\common\validate\Goods;

        if (!$validate->check($info)) {
            $res['code'] = 0;
            $res['msg'] = $validate->getError();
            return $res;
        }
          $goods = goodsModel::create($info);
         if($goods){
            $data['code'] = 1;
            $data['msg'] = '添加成功';
          }else{
            $data['code'] = 0;
            $data['msg'] = '添加失败';
          }
          return $data;


    }
    //更新商品信息
    public function detail(Request $request)
    {
        $id =  $request->param('id');
        if($id) {
            $info = goodsModel::where('id', $id)->find();
            $info['img1'] = explode(',', $info['img1']);
            $name = GoodsCategoryModel::where('cat_id', $info['cat_id'])->field('cat_name')->find();
            $info['cat_name'] = $name['cat_name'];
            $selectOption = GoodsCategoryModel::where('cat_id','<>',0)->order('cat_id asc')->select()->toArray();
            $this->assign('selectOption',$selectOption);
            $this->assign('info', $info);
            return $this->fetch();
        }
    }
    //操作更新商品信息
    public function dodetail(Request $request)
    {
        $info = $_POST;
        if($info['cat_id'] == 11){
            //如果商品类型为拼团秒杀
            if(!$info['fighting_price'] || $info['fighting_price'] <=0){
                $data['code'] = 0;
                $data['msg'] = '请填写拼团价';
                return $data;
            }
            if(!$info['people_count'] || $info['people_count'] <=0){
                $data['code'] = 0;
                $data['msg'] = '请填写拼团人数';
                return $data;
            }
            if(!$info['fight_time']){
                $data['code'] = 0;
                $data['msg'] = '请填写拼团到期时间';
                return $data;
            }
        }
        if($info['cat_id'] == 12){
            //如果商品类型为限时秒杀
            if(!$info['price_spike'] || $info['price_spike'] <=0){
                $data['code'] = 0;
                $data['msg'] = '请填写限时秒杀价';
                return $data;
            }
            if(!$info['start_time'] || $info['end_time'] <=0){
                $data['code'] = 0;
                $data['msg'] = '请填写限时时间';
                return $data;
            }
        }
        if($info['start_time']){
            $info['start_time'] = strtotime($info['start_time']);
        }
        if($info['end_time']){
            $info['end_time'] = strtotime($info['end_time']);
        }
        $info['play_start_time'] = strtotime($info['play_start_time']);
        $info['play_end_time'] = strtotime($info['play_end_time']);
        if(isset($info['pc_src'])){
            $info['img1'] = implode(",",$info['pc_src']);
            unset($info['pc_src']);
        }
        $info['detail'] = $info['content'];
        unset($info['content']);
        $validate = new \app\common\validate\Goods;

        if (!$validate->check($info)) {
            $res['code'] = 0;
            $res['msg'] = $validate->getError();
            return $res;
        }
        //更新
        $update = GoodsModel::where('id', $info['id'])->update($info);
        if($update){
                $data['code'] = 1;
                $data['msg'] = '修改成功';
        }else{
            $data['code'] = 0;
            $data['msg'] = '修改失败';
        }
        return $data;


    }

}
